package cn.itcast.oa.view.action;



import java.util.HashSet;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.oa.base.BaseAction;
import cn.itcast.oa.domain.Department;
import cn.itcast.oa.domain.Role;
import cn.itcast.oa.domain.User;
import cn.itcast.oa.util.DepartmentUtils;

import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User>{
	
	
	
	private static final long serialVersionUID = 1L;
	
	private Long departmentId;  //与部门表关联的外键id
	
	private Long[] roleIds;    //与岗位表关联的id数组

	public Long[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Long[] roleIds) {
		this.roleIds = roleIds;
	}

	public Long getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Long departmentId) {
		this.departmentId = departmentId;
	}

	/**列表*/
	public String list() throws Exception {
       List<User> userlist= userService.findAll();
	   ActionContext.getContext().put("userlist", userlist);
	   return "list";
	}
	
	/**删除*/
	public String delete() throws Exception {
		userService.delete(model.getId());
		return "tolist";
	}
	
	/**添加页面*/
	public String addUI() throws Exception {
		
		//准备数据departmentlist
		List<Department> topList=departmentService.findTopList();
		List<Department> departmentlist=DepartmentUtils.getAllDepartments(topList);
		
		ActionContext.getContext().put("departmentlist", departmentlist);
		
		//准备数据roleList
		List<Role> roleList=roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		
		return "saveUI";
	}
	
	/**添加*/
	public String add() throws Exception {
		//封装到对象中(当model是实体类型时，也可以使用model，但要设置未封装的属性)
		//设置关联的所属部门
		model.setDepartment(departmentService.getById(departmentId));
		//设置关联的所属岗位
		List<Role> rolelist= roleService.getByIds(roleIds);
		model.setRoles(new HashSet<Role>(rolelist));   //把list集合中的数据全部放到set集合中
		
		//设置密码为1234(使用MD5摘要)
		String md5Digest=DigestUtils.md5Hex("1234");
		model.setPassword(md5Digest);
		
		//保存到数据库中	
		userService.save(model);
		return "tolist";
	}
	
	
	/**修改页面*/
	public String editUI() throws Exception {
	
		//准备数据departmentlist
		List<Department> topList=departmentService.findTopList();
		List<Department> departmentlist=DepartmentUtils.getAllDepartments(topList);
		
		ActionContext.getContext().put("departmentlist", departmentlist);
		
		//准备数据roleList
		List<Role> roleList=roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		
		//准备回显的数据(一般属性数据)
		User user= userService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(user);
		
		//准备回显的部门和岗位数据(传送的参数id)
		if(user.getDepartment() != null){
			
			departmentId=user.getDepartment().getId();	
		}
		
		if(user.getRoles() != null){
			roleIds=new Long[user.getRoles().size()];
			int index=0;
			for(Role role:user.getRoles()){
				roleIds[index++] = role.getId();
			}
			
		}
		
		return "saveUI";
	}
	
	/**修改*/  
	public String edit() throws Exception {
		//1.从数据库中取出源对象
		User user = userService.getById(model.getId());
		//2.设置要修改的属性
		user.setName(model.getName());
		user.setLoginName(model.getLoginName());
		user.setDescription(model.getDescription());
		user.setEmail(model.getEmail());
		user.setGender(model.getGender());
		user.setPhoneNumber(model.getPhoneNumber());
		
		//设置关联的所属部门
		user.setDepartment(departmentService.getById(departmentId));
		//设置关联的所属岗位
		List<Role> rolelist= roleService.getByIds(roleIds);
		user.setRoles(new HashSet<Role>(rolelist));   //把list集合中的数据全部放到set集合中
		
		//3.更新到数据库中
		userService.update(user);
		return "tolist";
	}
	
	/**初始化密码为1234*/  
	public String initPassword() throws Exception {
		//1.从数据库中取出源对象
		User user = userService.getById(model.getId());
		//2.设置要修改的属性
		String md5Digest=DigestUtils.md5Hex("1234");
		user.setPassword(md5Digest);
		//3.更新到数据库中
		userService.update(user);
		return "tolist";
	}
	
	/*登录页面*/
	public String loginUI() throws Exception {
		
		return "loginUI";
	}
	
	/*登录*/
	public String login() throws Exception {
		User user= userService.findByLoginNameAndPassword(model.getLoginName(),model.getPassword());
			if(user == null){
				addFieldError("login", "用户名或密码不正确！");
				return "loginUI";
			}else{
				ActionContext.getContext().getSession().put("user", user);
				return "toIndex";
			}
		}
	/*注销*/
	public String logout() throws Exception {
		ActionContext.getContext().getSession().remove("user");
		return "logout";
	}

}
